<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const PENDING = "pending";
      const FULFILLED = "fulfilled";
      const REJECTED = "rejected";
      class MyPromise {
        constructor(executor) {
          executor(this.resolve, this.reject);
        }
        status = PENDING;
        value = undefined;
        reason = undefined;

        resolve = (value) => {
          if (this.status !== PENDING) {
            return;
          }
          this.status = FULFILLED;
          this.value = value;
        };

        reject = (reason) => {
          if (this.status !== PENDING) {
            return;
          }
          this.status = REJECTED;
          this.reason = reason;
        };
        then(successCallBack, failCallBack) {
          if (this.status == FULFILLED) {
            successCallBack(this.value);
          } else if (this.status === REJECTED) {
            failCallBack(this.reason);
          }
        }
      }
      let p = new MyPromise((resolve, reject) => {
        resolve("成功");
        reject("失败");
      });
      p.then(
        (res) => {
          console.log(res);
        },
        (err) => {
          console.log(err);
        }
      );
    </script>
  </body>
</html>
